import { createRouter, createWebHashHistory } from 'vue-router'

import Login from '../views/Login.vue'
import Index from '../views/Index.vue'
const routes = [
  {
    path: '/login',
    component: Login
  },
  {
    path: '/',
    component: Index
  },
  {
    path: '/register',
    component: () => import('../views/Register.vue')
  }
]

const router = createRouter({
  routes: routes,
  history: createWebHashHistory()
})

router.beforeEach((to, from, next) => {
    const token = localStorage.getItem('token')
    if(to.path === '/') {
        if(token) { next() }
        else {
            next('/login')
        }
    } else {
        next()
    }
})
export default router